<?php
$relPath="./../../pinc/";
require_once $relPath."dpinit.php";
require_once $relPath."dp_main.inc";
require_once $relPath."DpTable.class.php";

theme_header(_("Role Manager"), true);

if(! $User->IsSiteManager()) {
    echo _("You are not authorized to use this form.");
    theme_footer();
    exit;
}

$username       = Arg("username");
$oldusername    = Arg("oldusername");
$projectid      = ArgProjectId();
$submit         = Arg("submit");

if($submit != "") {
    // handle_roles($username);
}

if($username != "" && $Context->IsUser($username)) {

    // pay no attention to updates for username changes
    if($username == $oldusername) {
        $ugrant = ArgArray("ugrant");
        // dump($ugrant);

        $pgrant = ArgArray("pgrant");
    }

    $uroles = $dpdb->SqlRows("SELECT rolename 
                          FROM user_roles
                          WHERE username = '{$username}'");

    if(isset($ugrant)) {
        foreach($uroles as $row) {
            $val = $row['rolename'];
            if(! isset($ugrant[$val])) {
                $dpdb->SqlExecute("DELETE FROM user_roles
                                WHERE username = '$username'
                                AND rolename = '$val'");
            }
        }

        foreach($ugrant as $key => $value) {
            if($value == "on") {
                $sql = "
                INSERT INTO user_roles
                    (rolename, username, grantdate, grantedby)
                SELECT '$key', '$username', 
                            UNIX_TIMESTAMP(), u.username
                FROM users u
                LEFT JOIN user_roles ur 
                    ON ur.rolename = '$key'
                        AND u.username = ur.username
                WHERE u.username = '$username'
                    AND ur.id IS NULL";
                $dpdb->SqlExecute($sql);
            }
        }
    }

    if(isset($pgrant)) {
        $proles = $dpdb->SqlRows("
            SELECT rolename
            FROM user_project_roles
            WHERE username = '$username'
                AND projectid = '$projectid'");
        if(isset($pgrant)) {
            foreach($proles as $prow) {
                $val = $prow['rolename'];
                if(! isset($pgrant[$val])) {
                    $sql = "
                        DELETE FROM user_project_roles
                        WHERE username = '$username'
                            AND projectid = '$projectid'
                            AND rolename = '$val'";
                    $dpdb->SqlExecute($sql);
                }
            }

            foreach($pgrant as $key => $value) {
                if($value == "on") {
                    $sql = "
                        INSERT INTO user_project_roles
                            (rolename, username, projectid, 
                                    grantdate, grantedby)
                        SELECT '$key', '$username', '$projectid',
                                UNIX_TIMESTAMP(), u.username
                        FROM users u
                        LEFT JOIN user_project_roles ur 
                            ON ur.rolename = '$key'
                                AND ur.projectid = '$projectid'
                                AND u.username = ur.username
                        WHERE u.username = '$username'
                            AND ur.id IS NULL";
                    // dump($sql);
                    $dpdb->SqlExecute($sql);
                }
            }
        }
    }
}

$ugrows = $dpdb->SqlRows("
        CALL UserGlobalRoleChecklist('$username')");

if($projectid != "") {
    $uprows = $dpdb->SqlRows("
        CALL UserProjectRoleChecklist('$username', '$projectid')");
}

echo "
    <form name='user_role_form' target='' method='POST'>
    <input type='hidden' name='oldusername' id='oldusername'
                                            value='$username'>
    Username:
    <input type='text' id='username' name='username'
                                            value='$username'>
<input type='submit'/>\n";

if($username != "") {
    say("
        <div id='divuser'>
        <h2>Roles for user: $username</h2>\n");

    $tbl = new DpTable();
    $tbl->AddColumn("Role", "description");
    $tbl->AddColumn("Grant", "user_is", "chkugrant");
    $tbl->SetRows($ugrows);
    $tbl->EchoTable();

    echo "<hr>
        Project ID:
        <input type='text' id='project' name='projectid'
                            value='$projectid'>\n";
    if($projectid != "") {
        say("<h2>Roles for project: $projectid</h2>\n");

        $tbl = new DpTable();
        $tbl->AddColumn("Role", "description");
        $tbl->AddColumn("Grant", "user_is", "chkpgrant");
        $tbl->SetRows($uprows);
        $tbl->EchoTable();
    }

    say("
        </div>\n");
}

say("</form>\n");

theme_footer();
exit;

function chkugrant($val, $row) {
    // dump($row);
    $code = $row['rolename'];
    $checked = ($row['user_is'] == "1"
                ? " checked = 'checked'"
                : "");
    return "<input type='checkbox' name='ugrant[{$code}]' $checked>\n";
}

function chkpgrant($val, $row) {
    // dump($row);
    $code = $row['rolename'];
    $checked = ($row['user_is'] == "1"
                ? " checked = 'checked'"
                : "");
    return "<input type='checkbox' name='pgrant[{$code}]' $checked>\n";
}

/*
function handle_roles($username) {
    global $dpdb;

    // $roles = Arg("permit");
    // if(! is_array($roles))
        return;
    $dpdb->SqlExecute("
        DELETE FROM user_roles 
        WHERE username ='$username'");
    foreach($roles as $rolename => $val)
    {
        $dpdb->SqlExecute("
            INSERT INTO user_roles
                (username, rolename)
            VALUES
                ('$username', '$rolename')");
    }
}
*/
?>
